#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2018, Lime Technology
 * Copyright 2012-2018, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$var   = parse_ini_file("/var/local/emhttp/var.ini");
$disks = parse_ini_file("/var/local/emhttp/disks.ini",true);

$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/webGui/include/CustomMerge.php";

$notify = "$docroot/webGui/scripts/notify";
$unraid = parse_plugin_cfg("dynamix",true);
$output = $unraid['notify']['report'];
$server = strtoupper($var['NAME']);
$data   = [];
$parity = false;
$cache  = false;
$error0 = 0;
$error1 = 0;
$error2 = 0;
$error3 = 0;

function plus($val, $word, $last) {
  return $val>0 ? (($val || $last) ? ($val.' '.$word.($val!=1?'':'').($last ?'':', ')) : '') : '';
}
function my_temp($value) {
  global $unraid;
  if ($value=='*') return ' - standby';
  $unit = $unraid['display']['unit'];
  return ' - active '.($unit=='F' ? round(9/5*$value+32) : str_replace('.', $unraid['display']['number'][0], $value)).' '.$unit;
}
function my_disk($name) {
  return ucfirst(preg_replace('/^(disk|cache)([0-9]+)/','$1 $2',$name));
}
function my_scale($value, &$unit, $precision = NULL) {
  global $unraid;
  $scale = $unraid['display']['scale'];
  $number = $unraid['display']['number'];
  $units = ['B','KB','MB','GB','TB','PB'];
  if ($scale==0 && $precision===NULL) {
    $unit = '';
    return number_format($value, 0, $number[0], ($value>=10000 ? $number[1] : ''));
  } else {
    $base = $value ? floor(log($value, 1000)) : 0;
    if ($scale>0 && $base>$scale) $base = $scale;
    $value = round($value/pow(1000, $base), $precision===NULL ? 2 : $precision);
    if ($value>=1000 && $scale<0) { $value = 1; $base++; }
    $unit = $units[$base];
    return number_format($value, $precision===NULL ? (($value-intval($value)==0 || $value>=100) ? 0 : ($value>=10 ? 1 : 2)) : $precision, $number[0], ($value>=10000 ? $number[1] : ''));
  }
}
function my_check($time,$speed) {
  if (!$time) return '不可用 (没有奇偶校验记录)';
  $days = floor($time/86400);
  $hmss = $time-$days*86400;
  $hour = floor($hmss/3600);
  $mins = $hmss/60%60;
  $secs = $hmss%60;
  return plus($days,'天',($hour|$mins|$secs)==0).plus($hour,'小时',($mins|$secs)==0).plus($mins,'分钟',$secs==0).plus($secs,'秒',true).". 平均速度: $speed";
}
function my_time($time) {
  global $unraid;
  $date = strftime($unraid['display']['date'].($unraid['display']['date']!='%c' ? ", {$unraid['display']['time']}" : ""), $time);
  $now  = new DateTime("@".intval(time()/86400)*86400);
  $last = new DateTime("@".intval($time/86400)*86400);
  $days = date_diff($last,$now)->format('%a');
  switch (true) {
  case ($days<0):
    return $date;
  case ($days==0):
    return "$date (天)";
  case ($days==1):
    return "$date (昨天)";
  default:
    return "$date ($days 天前)";
  }
}
function my_clock($time) {
  if (!$time) return '不到一分钟';
  $days = floor($time/1440);
  $hour = $time/60%24;
  $mins = $time%60;
  return plus($days,'天',($hour|$mins)==0).plus($hour,'小时',$mins==0).plus($mins,'分钟',true);
}

function my_array(&$disk) {
  global $data,$unraid,$error0,$error1,$error2,$error3;
  $name = $disk['name'];
  $hot  = strlen($disk['hotTemp']) ? $disk['hotTemp'] : $unraid['display']['hot'];
  $max  = strlen($disk['maxTemp']) ? $disk['maxTemp'] : $unraid['display']['max'];
  if (strpos($disk['status'],'_NP')!==false) return false;
  $temp = $disk['temp'];
  if ($temp>=$max) {
    $fail = ' (磁盘过热';
    $error0++;
  } elseif ($temp>=$hot) {
    $fail = ' (磁盘很热';
    $error1++;
  } else {
    $fail = '';
  }
  if ($disk['numErrors']>0) {
    if ($fail) $fail .= ', '; else $fail = ' (';
    $fail .= '磁盘有读取错误';
    $error2++;
  }
  if ($fail) $fail .= ')';
  $status = $fail ? ' [NOK]' : ' [OK]';
  $color = strtok($disk['color'],'-');
  if ($color=='red'||$color=='yellow') { $error3++; $status = ' ['.str_replace(['NP_','_'],['',' '],$disk['status']).']'; }
  $info = "{$disk['id']} ({$disk['device']})";
  if ($info==" ()") $info = '不存在设备标识';
  $data[] = my_disk($name)." - $info".my_temp($temp).$fail.$status;
  return true;
}

// generate report of array devices
foreach ($disks as $disk) if ($disk['type']=='Parity') $parity |= my_array($disk);
foreach ($disks as $disk) if ($disk['type']=='Data') my_array($disk);
foreach ($disks as $disk) if ($disk['type']=='Cache') $cache |= my_array($disk);

$size = count($data);

// generate parity report
$data[] = "";
$mdResync = $var['mdResync'];
if ($mdResync>0) {
  $mdResyncPos = $var['mdResyncPos'];
  $mdResyncDb = $var['mdResyncDb'];
  $mdResyncDt = $var['mdResyncDt'];
  $mode = '';
  if (strstr($var['mdResyncAction'],"recon")) {
    $mode = '奇偶同步/数据重建';
  } elseif (strstr($var['mdResyncAction'],"clear")) {
    $mode = '磁盘清理';
  } elseif ($var['mdResyncAction']=="check") {
    $mode = '读取检查';
  } elseif (strstr($var['mdResyncAction'],"check")) {
    $mode = '奇偶校验';
  }
  $data[] = $mode." 正在进行中.";
  $data[] = "总大小: ".my_scale($mdResync*1024, $unit)." $unit";
  $data[] = "经过的时间: ".my_clock(floor((time()-$var['sbUpdated'])/60));
  $data[] = "当前位置: ".my_scale($mdResyncPos*1024, $unit)." $unit (".number_format(($mdResyncPos/($mdResync/100+1)),1,$unraid['display']['number'][0],'')." %)";
  $data[] = "估计速度: ".my_scale($mdResyncDb/$mdResyncDt*1024, $unit, 1)." $unit/秒";
  $data[] = "估计完成时间: ".my_clock(round(((($mdResyncDt*(($mdResync-$mdResyncPos)/($mdResyncDb/100+1)))/100)/60),0));
  $data[] = "同步错误 ".($var['mdResyncCorr']==0 ? 'detected: ' : 'corrected: ').$var['sbSyncErrs'];
} else {
  $sbSynced = $var['sbSynced'];
  $sbSynced2 = $var['sbSynced2'];
  $sbSyncErrs = $var['sbSyncErrs'];
  if ($var['sbSyncExit']!=0) {
    $data[] = "上次检查未完成 ".my_time($sbSynced2).", 发现 $sbSyncErrs 个错误".($sbSyncErrs==1?'.':' ');
    $data[] = "错误代码: ".$var['sbSyncExit'];
  } elseif ($sbSynced==0) {
    $data[] = "奇偶尚未检查";
  } elseif ($sbSynced2>0) {
    if (strstr($var['mdResyncAction'],"recon")) {
      $data[] = '奇偶或数据无效';
    } else {
      $data[] = '奇偶有效';
    }
    $duration = $sbSynced2 - $sbSynced;
    $speed = my_scale($var['mdResyncSize']*1024/$duration,$unit,1)." $unit/秒";
    $data[] = "上次检查时间 ".my_time($sbSynced2).", 发现 $sbSyncErrs 个错误".($sbSyncErrs==1?'.':' ');
    $data[] = "持续时间: ".my_check($duration,$speed);
  }
}

$word = $size==1 ? "" : "including ";
$warn = ($error0 || $error3) ? "alert" : (($error1 || $error2) ? "warning" : "normal");
$stat = $warn=="normal" ? "[PASS]" : "[FAIL]";
$info = "阵列 $size 硬盘".($size==1 ? "" : "").($parity ? " ({$word}parity".($cache ? " & cache)" : ")") : ($cache ? " ({$word}cache)" : ""));
$message = implode('\n', $data);
exec("$notify -s ".escapeshellarg("通知 [$server] - 阵列健康报告 $stat")." -d ".escapeshellarg("$info")." -m ".escapeshellarg("$message")." -i ".escapeshellarg("$warn $output"));

exit(0);
?>
